package idpa.paul.hangman.view;

import java.util.Random;

import idpa.paul.hangman.controller.NewGameController;
import idpa.paul.hangman.controller.guessLetterController;
import idpa.paul.hangman.model.hangmanModel;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {

	//variables
	
	public EditText guessLetter;
	public LinearLayout wordDisplay;
	public ImageView hangmanImage;
	public Button submitButton;
	public TextView[] letters;
	public hangmanModel model;
	public TextView display_guessedLetters;
	public String chosenWord;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = this.getIntent();
        chosenWord = intent.getStringExtra("Word");
        
        
        guessLetter = (EditText) findViewById(R.id.guess_editText);
        wordDisplay = (LinearLayout) findViewById(R.id.display_linearLayout);
        hangmanImage = (ImageView) findViewById(R.id.hangman_imageView);
        submitButton = (Button) findViewById(R.id.submit_button);
        model = new hangmanModel(chosenWord);
        letters = new TextView[model.length()];
        display_guessedLetters = (TextView) findViewById(R.id.display_guessedLetters);
        
        display_guessedLetters.setText(" ");
        
        hangmanImage.setImageResource(R.drawable.board5);
        
        LayoutParams Lparams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        for(int i =0; i<letters.length; i++){
        	
        	TextView tv = new TextView(MainActivity.this);
        	tv.setLayoutParams(Lparams);
        	tv.setText(model.getLetter(i)+"  ");
        	wordDisplay.addView(tv);
        	letters[i]=tv;
        	
        }
        
        submitButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				new guessLetterController(model, MainActivity.this).process();
				
			}
		});
        
        
        
        
    }
    


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        //getMenuInflater().inflate(R.menu.main, menu);
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.hangman_menu, menu);
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	
    		switch(item.getItemId()){
    		
    		case R.id.new_game:
    			new NewGameController(model, MainActivity.this).process();
    			return true;
    		case R.id.quit:
    			MainActivity.this.finish();
    			return true;
    		case R.id.help:
    			
    			return true;
    		case R.id.about:
    			
    			return true;
    		default:
    			return super.onOptionsItemSelected(item);
    		
    		
    		}
			
			
    		
    }
    
    
}
